<?php
require_once "src/apiClient.php";
require_once "src/contrib/apiCalendarService.php";
session_start();
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Gadmin extends CI_Controller {
function __construct()
    {
        parent::__construct();
    	$this->load->library('session');
		$this->load->helper('url');
    	$this->load->database();
    	$this->load->model("Adminmod");
    	$this->load->model("centermod");
    	$this->load->model("modelingmod");
    	
	}
	
/************* list *****************************/
		function apigooglecalarapi()
	{
			$client = new apiClient();
			$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
 $client->setClientId('770938131025.apps.googleusercontent.com');
$client->setClientSecret('bOih7XMjWC6E5M18eaGSV8EI');
 $client->setRedirectUri('http://localhost:8888/pshowmanager/index.php/add/sendevent');

$client->setDeveloperKey('AI39si6a39ey9Zl_elSjD3eTiJV7Aiav2FK3luNQweZGdvLckqPGINxRF349spYyrzESSINdA3kOQgQknnROREMKhzOtNg0Zvw');
$client->setUseObjects(true);
$cal = new apiCalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
	 header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
		
	return $client;
	}
	
	
	
	
	
	
function bookinglistadmin()
{
	if($this->session->userdata("user")==false && $this->session->userdata("id")<=0&& $this->session->userdata("role")!="admin")
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$listbooking=$this->Adminmod->getuserappointafternow();
				$listphotograph=$this->Adminmod->getlistphotograph();
				$listphotoassigned=$this->Adminmod->getuserappointassigned();
				$listphotodone=$this->Adminmod->photoshowdonelist();
				$data=array("listbooking"=>$listbooking,"listphotograph"=>$listphotograph,"listphotoassigned"=>$listphotoassigned,"listphotodone"=>$listphotodone);
				$this->load->view("admin/listbookingbyadmin",$data);	
						
			}
}


function modelinglistadmin()
{
	
	if($this->session->userdata("user")==false && $this->session->userdata("id")<=0&& $this->session->userdata("role")!="admin")
			{
					header('Location:index.php?pages/loginadmin');
						
			}else {
						$listallreadypica=$this->Adminmod->listpicasready();
						
					$listpgraph=$this->getnompgraph();
					$listmodelingassgined=$this->Adminmod-> allmodelingassigned();
					$allmodelingfinished=$this->Adminmod->allmodelingfinished();
					$listmoderateur=$this->getnommoderateur();
					$data=array("listallreadypica"=>$listallreadypica,"listpgraph"=>$listpgraph,"listmodelingassgined"=>$listmodelingassgined
								,"allmodelingfinished"=>$allmodelingfinished,"listmoderateur"=>$listmoderateur);
					$this->load->view("admin/listbymodelingbyadmin",$data);	
						
				  }

}



function getnompgraph()
{
	$listreturn=array();
	$listpgraph=$this->modelingmod->getlistphotograph();
	foreach ($listpgraph as $pg)
	{
		$listreturn[$pg['iduser']]=$pg['name'].' '.$pg['lastname'];	
	}
	return $listreturn;
}

function getnommoderateur()
{
	$listreturn=array();
	$listpgraph=$this->Adminmod->getlistmoderateur();
	foreach ($listpgraph as $pg)
	{
		$listreturn[$pg['iduser']]=$pg['name'].' '.$pg['lastname'];	
	}
	return $listreturn;
}
/************* end list *****************************/


/*************************** Add ********************/

	function actionassingnephoto()
	{
		if($this->session->userdata("user")==false && $this->session->userdata("id")<=0&& $this->session->userdata("role")!="admin")
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$idappointments=$this->input->get_post("idappointments");
				$role=$this->input->get_post("role");
				$idphotograph=$this->input->get_post("idphotograph");
				$infophotograph=$this->Adminmod->getphotographinfo($idphotograph);
				$bookingshowinfo=$this->Adminmod->getuserappointbyidpointement($idappointments);
				
				$idgooglecalendor=$bookingshowinfo[0]["idgooglecalendor"];
				$idcolor=$infophotograph[0]["idcolor"];
			
			//changecolor google calendar
			$client=$this->apigooglecalarapi();
			$cal = new apiCalendarService($client);
			if ($client->getAccessToken()) {
 			
			//-------update event--------

			$calendarid=$this->centermod->getoption("calendarid");
			$event = $cal->events->get($calendarid, $idgooglecalendor);
			$event->setColorId($idcolor);
			$cal->events->update($calendarid, $event->getId(), $event);
			//echo $createdEvent->getId()."you booking addappoitment";		
			}
				
				$data=array("User_idUser"=>$idphotograph,"statusshooting_idstatusshooting"=>1,"appointments_idappointments"=>$idappointments);
				$idinsert2=$this->Adminmod-> insertetemplate("shooting",$data);	
				redirect("index.php?gadmin/bookinglistadmin",'refresh');
			
			
			}
	}


	
	function editassingnephoto()
	{
		if($this->session->userdata("user")==false && $this->session->userdata("id")<=0&& $this->session->userdata("role")!="admin")
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$idappointments=$this->input->get_post("idappointments");
				$role=$this->input->get_post("role");
				$idphotograph=$this->input->get_post("idphotograph");
				$idshooting=$this->input->get_post("idshooting");
				
				$infophotograph=$this->Adminmod->getphotographinfo($idphotograph);
				$bookingshowinfo=$this->Adminmod->getuserappointbyidpointement($idappointments);
				
				$idgooglecalendor=$bookingshowinfo[0]["idgooglecalendor"];
				$idcolor=$infophotograph[0]["idcolor"];
				//changecolor google calendar
				$client=$this->apigooglecalarapi();
				$cal = new apiCalendarService($client);
				if ($client->getAccessToken()) {
	 			
				//-------update event--------
	
				$calendarid=$this->centermod->getoption("calendarid");
				$event = $cal->events->get($calendarid, $idgooglecalendor);
				$event->setColorId($idcolor);
				$cal->events->update($calendarid, $event->getId(), $event);
				//echo $createdEvent->getId()."you booking addappoitment";		
				}
				
				
				$data=array("User_idUser"=>$idphotograph);
				$idinsert2=$this->Adminmod-> updatetemplate("shooting",$data,"idshooting=$idshooting");
				redirect("index.php?gadmin/bookinglistadmin",'refresh');
			
			}
	}
/***************************** end Add ***************/

}//fin class